package com.ruoyi.project.tool.backupSql.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.support.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.tool.backupSql.service.SqlService;
import com.ruoyi.project.tool.backupSql.util.ExportSql;
import com.ruoyi.project.tool.gen.domain.TableInfo;
import com.ruoyi.project.tool.gen.service.IGenService;

/**
 * 代码生成 操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/tool/backupSql")
public class SqlController extends BaseController {
	private String prefix = "tool/backupSql";

	@Autowired
	private SqlService sqlService;

	@RequiresPermissions("tool:backupSql:view")
	@GetMapping()
	public String gen() {
		return prefix + "/backupSql";
	}

	@RequiresPermissions("tool:backupSql:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TableInfo tableInfo) {
		startPage();
		List<TableInfo> list = sqlService.selectTableList(tableInfo);
		return getDataTable(list);
	}

	/**
	 * 备份数据库
	 * 
	 * @param response
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("tool:backupSql:download")
	// @RequestMapping("/down")
	@GetMapping("/download/{tableName}")
	@ResponseBody
	public String down(HttpServletResponse response, @PathVariable("tableName") String tableName) throws Exception {

		System.out.println("---------开始备份数据库-------");
		ExportSql exportSql = new ExportSql();
		exportSql.exportSql(tableName, "root", "123456789", "ry");
		System.out.println("---------备份结束-------");

		List<File> files = new ArrayList<File>();
		File file1 = new File("C:\\Mysql\\" + tableName + ".sql");
		files.add(file1);
		exportSql.downLoadFiles(files, response);
		return "成功";
	}

	/**
	 * 批量生成代码
	 * 
	 * @throws Exception
	 */
	@RequiresPermissions("tool:backupSql:download")
	// @Log(title = "代码生成", businessType = BusinessType.GENCODE)
	@GetMapping("/downloads")
	@ResponseBody
	public String batchGenCode(HttpServletResponse response, String tables) throws Exception {
		String[] tableNames = Convert.toStrArray(tables);

		System.out.println("---------开始备份数据库-------");
		ExportSql exportSql = new ExportSql();
		for (int i = 0; i < tableNames.length; i++)
			exportSql.exportSql(tableNames[i], "root", "123456789", "ry");
		System.out.println("---------备份结束-------");

		List<File> files = new ArrayList<File>();
		for (int i = 0; i < tableNames.length; i++) {
			files.add(new File("C:\\Mysql\\" + tableNames[i] + ".sql"));
		}
		// File file1 = new File("D:\\Mysql\\"+tableNames[i]+".sql");
		// files.add(file1);
		// Thread.sleep(2000);
		exportSql.downLoadFiles(files, response);
		return "成功";
	}

}
